package msb.class02;

import org.junit.Assert;
import org.junit.Test;
import common.util.ArrayUtil;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 如何用队列结构实现栈
 * <p>
 * 实现：
 * 2个队列倒腾下
 */
public class Code08_CodeStackWithQueue {
    private LinkedList<Integer> queue1 = new LinkedList<>();
    private LinkedList<Integer> queue2 = new LinkedList<>();

    public void push(Integer num) {
        while (!queue2.isEmpty()) {
            queue1.add(queue2.poll());
        }
        queue1.add(num);
    }

    public Integer pop() {
        while (!queue2.isEmpty()) {
            queue1.add(queue2.poll());
        }
        while (queue1.size() > 1 ) {
            queue2.add(queue1.poll());
        }

        return queue1.poll();
    }

    @Test
    public void test() {
        int[] instant = ArrayUtil.instant(100, 100);
        System.out.println(Arrays.toString(instant));
        Stack<Integer> stack = new Stack<>();
        Code08_CodeStackWithQueue code08_codeStackWithQueue = new Code08_CodeStackWithQueue();
        for (int i : instant) {
            stack.push(i);
            code08_codeStackWithQueue.push(i);
        }
        for (int i : instant) {

            Assert.assertEquals(stack.pop(), code08_codeStackWithQueue.pop());
        }

    }

}
